export class MultiMap<T, K> extends Map<T, K[]> {

    private _Count: number = 0;

    public Add(key: T, value: K): void {
        let list = this.TryGetValue(key);
        if (list == null) {
            list = [];
            this.AddList(key, list);
        }
        list.push(value);
    }

    public AddList(key: T, value: K[]): void {
        this.set(key, value);
        this._Count++;
    }

    public Remove(key: T): boolean;
    public Remove(key: T, value: K[]): boolean;
    public Remove(key: T, value?: K[]): boolean {
        value = this.TryGetValue(key);
        if (value) {
            this.delete(key);
            this._Count--;
            return true;
        }
        return false;
    }

    public RemoveValue(key: T) {
        let value = this.TryGetValue(key);
        if (value) {
            this.Remove(key);
        }
        return value;
    }

    public Clear() {
        this.clear();
    }

    public TryGetValue(key: T): K[] {
        if (this.ContainsKey(key)) {
            return this.get(key);
        }
        return null;
    }

    public ContainsKey(key: T) {
        return this.has(key);
    }

    public get Count() {
        return this._Count;
    }

}